package com.key.util.interceptot;

import java.util.Map;

import com.key.org.model.Person;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {

		// 取得请求相关的ActionContext实例
		ActionContext ctx = invocation.getInvocationContext();
		Map session = ctx.getSession();
		Person person = (Person) session.get("Person");
		if (person == null) {
			setGoingToURL(session,ctx.getActionInvocation());
			return "Login";
		}
		// session
		return invocation.invoke();
	}

	private void setGoingToURL(Map session, ActionInvocation invocation) {
		String url = "";
		String namespace = invocation.getProxy().getNamespace();
		if (!namespace.equals("") && !namespace.equals("/")) {
			url = url + namespace;
		}
		String actionName = invocation.getProxy().getActionName();
		if (!actionName.equals("")) {
			url = url + "/" + actionName + ".do";
		}
		session.put("GOING_TO_URL", url);
	}
}